﻿using BGYXControlLibrary.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace BGYXControlLibrary.Common.Layer
{
    public static class LayerUtil
    {
        private static Dictionary<Window, Grid> S_windowGrid = new Dictionary<Window, Grid>();
        private static Dictionary<Window, Control> S_windowContent = new Dictionary<Window, Control>();

        public static void Init(Window window)
        {
            var contentPresenter = BGYX_VisualTreeHelper.GetParent<ContentPresenter>((UIElement)window.Content);

            if (contentPresenter == null)
            {
                throw new Exception("请将Init方法放在窗口初始化完毕后执行，比如窗口Loaded事件。");
            }

            Grid grid = new Grid();
            BGYX_Alert content = new BGYX_Alert() { Visibility = Visibility.Collapsed };

            S_windowGrid.Add(window, grid);
            S_windowContent.Add(window, content);

            if (contentPresenter.Parent is Panel)
            {
                var panel = ((Panel)contentPresenter.Parent);
                List<UIElement> uiList = panel.Children.Cast<UIElement>().ToList();
                panel.Children.Clear();
                panel.Children.Add(grid);
                foreach (var ele in uiList)
                {
                    grid.Children.Add(ele);
                }
                grid.Children.Add(content);
            }
            else if (contentPresenter.Parent is Decorator)
            {
                var decorator = ((Decorator)contentPresenter.Parent);
                var tempUI = decorator.Child;
                decorator.Child = grid;
                grid.Children.Add(tempUI);
                grid.Children.Add(content);
            }
            else
            {
                throw new Exception("该窗口不支持方法!");
            }
        }

        private static Control GetAlertContent(UIElement currentElement)
        {
            var window = Window.GetWindow(currentElement);
            if (!S_windowContent.ContainsKey(window))
            {
                Init(window);
            }
            return S_windowContent[window];
        }

        //private static Grid GetAlert(UIElement currentElement)
        //{
        //    var window = Window.GetWindow(currentElement);
        //    if (!S_windowGrid.ContainsKey(window))
        //    {
        //        Init(window);
        //    }
        //    return S_windowGrid[window];
        //}

        public static void Alert(this UIElement currentElement, string text)
        {
            var alertContent = GetAlertContent(currentElement) as BGYX_Alert;
            if (alertContent != null)
            {
                alertContent.IsLoading = false;
                alertContent.Content = text;
                alertContent.ContentWidth = 500;
                alertContent.ContentHeight = 300;
                alertContent.Visibility = Visibility.Visible;
            }
        }

        public static void ShowLoading(this UIElement currentElement, Icons loadType = Icons.FONTAWESOME_SOLID_0, RotateDirection direction = RotateDirection.Right)
        {
            var alertContent = GetAlertContent(currentElement) as BGYX_Alert;
            if (alertContent != null)
            {
                alertContent.LoadType = loadType;
                alertContent.IsLoading = true;
                alertContent.Direction = direction;
                alertContent.ContentWidth = 100;
                alertContent.ContentHeight = 100;
                alertContent.Visibility = Visibility.Visible;
            }
        }

        public static void AlertClose(this UIElement currentElement)
        {
            var alertContent = GetAlertContent(currentElement) as BGYX_Alert;
            if (alertContent != null)
            {
                alertContent.IsLoading = false;
                alertContent.Visibility = Visibility.Collapsed;
            }
        }
    }
}
